package org.boun.swe.ARCEONE.Adapter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.boun.swe.ARCEONE.R;
import org.boun.swe.ARCEONE.ViolationActivity;
import org.boun.swe.ARCEONE.Data.ViolationObject;
import org.boun.swe.ARCEONE.Static.Constants;
import org.boun.swe.ARCEONE.Utility.JSONManager;
import org.boun.swe.ARCEONE.Utility.StorageManager;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViolationListItemAdapter extends BaseAdapter{
	private JSONManager jsonManager;
	private StorageManager storageManager;
	private List<ViolationObject> violationList;
	private Context context;
	private ViolationListCell cell;
	private List<NameValuePair> nameValuePair;
	private boolean unfollowEnabled;
	
	public ViolationListItemAdapter(List<ViolationObject> list, Context context, boolean enableUnfollow) {
		violationList = list;
		this.context = context;
		unfollowEnabled = enableUnfollow;
		
		jsonManager = new JSONManager(context);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
	    View rowView = convertView;  
	    final ViolationObject violationObject = violationList.get(position);
	    
		if(rowView == null){
			cell = new ViolationListCell();
			LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        rowView = layoutInflater.inflate(R.layout.violation_list_item, null);
	        cell.cellTitle = (TextView)rowView.findViewById(R.id.violationListItemViolationTitle);
	        cell.cellLocation = (TextView)rowView.findViewById(R.id.violationListItemViolationLocation);
	        cell.firstCellButton = (Button)rowView.findViewById(R.id.violationListItemGoToViolationButton);
	        cell.secondCellButton = (Button)rowView.findViewById(R.id.violationListItemUnfollowButton);
	        if(!unfollowEnabled){
	        	cell.secondCellButton.setVisibility(View.INVISIBLE);
				android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)cell.firstCellButton.getLayoutParams();
				layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
				cell.firstCellButton.setLayoutParams(layoutParams);
	        }
	        cell.cellCoordinates = (TextView)rowView.findViewById(R.id.violationListItemViolationCoordinates);
	        
	        cell.firstCellButton.setOnClickListener(new View.OnClickListener() {
	        	public void onClick(View view) {
					Intent intent = new Intent(context, ViolationActivity.class);
					intent.putExtra(Constants.TEMPORARY_VIOLATION_ID, violationObject.ID);
					context.startActivity(intent);
	            }
	        });
	        cell.secondCellButton.setOnClickListener(new View.OnClickListener() {
	        	public void onClick(View view) {
	        		cell.secondCellButton.setVisibility(View.INVISIBLE);

	        		nameValuePair = new ArrayList<NameValuePair>();
					nameValuePair.add(new BasicNameValuePair("userName", (String)storageManager.getStoredData(Constants.PERSISTENT_USERNAME, String.class)));
					nameValuePair.add(new BasicNameValuePair("id", String.format("%d", violationObject.ID)));
					
					jsonManager.PostJSONData(Constants.POST_UNFOLLOW_VIOLATION, nameValuePair);
	            }
	        });
	        rowView.setTag(cell);
		}
		else
			cell = (ViolationListCell)rowView.getTag();
	
	    cell.cellTitle.setText(violationObject.Title == null ? "" : violationObject.Title);
	    if(violationObject.Location != null){
	    	cell.cellLocation.setText(violationObject.Location.Description == null ? "" : violationObject.Location.Description);
	    	cell.cellCoordinates.setText((violationObject.Location.Latitude == null ? "" : violationObject.Location.Latitude) + ";" + (violationObject.Location.Longitude == null ? "" : violationObject.Location.Longitude));
	    }
	    return rowView;
	}
	
	@Override
	public int getCount() {
		return violationList.size();
	}
	
	@Override
	public Object getItem(int position) {
		return violationList.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void ToggleCheckBox(int position){
		violationList.get(position);
	}
}
